package com.estun.backroundManagementService.modules.alarm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.alarm.model.StationAlarm;
import com.estun.backroundManagementService.modules.alarm.service.StationAlarmService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/alarm")
@RequiredArgsConstructor
public class AlarmController {

    private final StationAlarmService stationAlarmService;


    //分页查询告警信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<StationAlarm> pageRequest) {
        Page<StationAlarm> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<StationAlarm> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<StationAlarm> page1 = stationAlarmService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }


    // 根据ID获取告警信息
    @GetMapping("/{id}")
    public BaseResponse getAlarmById(@PathVariable String id) {
        return BaseResponse.success(stationAlarmService.getById(id));
    }

    // 添加告警信息
    @PostMapping("/add")
    @Log(module = "告警主数据 工厂ems同步", type = LogConstants.INSERT, desc = "告警主数据 工厂ems同步")
    public BaseResponse addAlarm(@RequestBody StationAlarm stationAlarm) {
        return BaseResponse.success(stationAlarmService.save(stationAlarm));
    }

    // 更新告警信息
    @PutMapping("/update")
    @Log(module = "告警主数据 更新", type = LogConstants.UPDATE, desc = "告警主数据 更新")
    public BaseResponse updateAlarm(@RequestBody StationAlarm stationAlarm) {
        return BaseResponse.success(stationAlarmService.updateById(stationAlarm));
    }

    // 删除告警信息
    @DeleteMapping("/delete/{id}")
    @Log(module = "告警主数据 删除", type = LogConstants.DELETE, desc = "告警主数据 删除")
    public BaseResponse deleteAlarm(@PathVariable String id) {
        return BaseResponse.success(stationAlarmService.removeById(id));
    }


    @ApiOperation("查询所有告警")
    @GetMapping(value = "/list")
    @ResponseBody
    public BaseResponse list() {
        return BaseResponse.success(stationAlarmService.list());
    }
}
